﻿@model MasResourceItem

@{
    var resSubGrp = ViewBag.resSubGrp;
    var masUnits = new KPIS.GERP.WEB.GIMS.MasUnitService.MasUnitServiceClient().SelectMasUnit(new MasUnit{RecordStatus = "!D"}, null);
    var masUnitsSelectList = from u in masUnits select new SelectListItem{Text = u.Name, Value = u.UnitId.ToString()};
    int resSubGrpId = Convert.ToInt32(ViewBag.resSubGrpId);
    int resItemId = Model == null ? 0 : Model.ResItemId;
    
}

@if (ViewContext.RouteData.Values["action"].ToString().ToLower() == "edit"){@Html.HiddenFor(model => model.ResItemId)}
@Html.Hidden("ResSubGrpId", resSubGrpId)
@Html.HiddenFor(model => model.RecordStatus)
@Html.HiddenFor(model => model.CreatedBy)
@Html.HiddenFor(model => model.CreatedWhen)
@Html.HiddenFor(model => model.UpdatedBy)
@Html.HiddenFor(model => model.UpdatedWhen)

<div class="widget-box">
    <div class="widget-header header-color-blue2">
        <h4 class="lighter smaller">รายการพัสดุ</h4>
    </div>
    <div class="widget-body">
        <div class="widget-body-inner">
            <div class="widget-main">
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.Code)
                    </div>
                    <div class="controls">
                        
                        <input class="text-box single-line" id="Code" name="Code" type="text"
                               value="@(Model != null ? Model.Code : KPIS.GERP.WEB.GIMS.Controllers.MasResourceItemUtil.GenerateNewCode(resSubGrpId))">
                         
                        <span id="xxxxxxxx" onclick="genNew();" style="cursor: pointer; background-color: rgb(198, 198, 233); padding: 10px 20px 10px 20px;">Gen. รหัสไม่ซ้ำใหม่</span>
                        <span id="yyyyyy" onclick="checkCode();" style="cursor: pointer; background-color: rgb(198, 198, 233); padding: 10px 20px 10px 20px; ">ตรวจสอบรหัส</span>
                        <br/>
                        <span id="message_code_generate" style="color:red"></span>
                        @Html.ValidationMessageFor(model => model.Code)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.OldCode)
                    </div>
                    <div class="controls">
                        @Html.EditorFor(model => model.OldCode)
                        @Html.ValidationMessageFor(model => model.OldCode)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.Name)
                    </div>
                    <div class="controls">
                        @Html.EditorFor(model => model.Name)
                        @Html.ValidationMessageFor(model => model.Name)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.Descr)
                    </div>
                    <div class="controls">
                        @Html.EditorFor(model => model.Descr, "TextArea")
                        @Html.ValidationMessageFor(model => model.Descr)
                    </div>
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.ResSubGrpId)
                    </div>
                    <div class="controls">
                        @resSubGrp.Code - @resSubGrp.Name
                        @Html.ValidationMessageFor(model => model.ResSubGrpId)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.GpscCode)
                    </div>
                    <div class="controls">
                        @Html.EditorFor(model => model.GpscCode)
                        @Html.ValidationMessageFor(model => model.GpscCode)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.GiinCode)
                    </div>
                    <div class="controls">
                        @Html.EditorFor(model => model.GiinCode)
                        @Html.ValidationMessageFor(model => model.GiinCode)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.StdPrice)
                    </div>
                    <div class="controls">
                        @Html.TextBoxFor(model => model.StdPrice, new {@class="input-mini spinner-input", maxlength="30" })
                        @Html.ValidationMessageFor(model => model.StdPrice)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.MinPrice)
                    </div>
                    <div class="controls">
                        @Html.TextBoxFor(model => model.MinPrice, new {@class="input-mini spinner-input", maxlength="30" })
                        @Html.ValidationMessageFor(model => model.MinPrice)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.MaxPrice)
                    </div>
                    <div class="controls">
                        @Html.TextBoxFor(model => model.MaxPrice, new {@class="input-mini spinner-input", maxlength="30" })
                        @Html.ValidationMessageFor(model => model.MaxPrice)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.UnitId)
                    </div>
                    <div class="controls">
                        @Html.DropDownListFor(m => m.UnitId
                                              , selectList: masUnitsSelectList
                                              , htmlAttributes: new Dictionary<string, object>{{"class","chzn-select"}, {"data-placeholder", "เลือกหน่วยนับ"} })
                        @Html.ValidationMessageFor(model => model.UnitId)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.MinQty)
                    </div>
                    <div class="controls">
                        @Html.TextBoxFor(model => model.MinQty, new {@class="input-mini spinner-input", maxlength="30" })
                        @Html.ValidationMessageFor(model => model.MinQty)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.MaxQty)
                    </div>
                    <div class="controls">
                        @Html.TextBoxFor(model => model.MaxQty, new {@class="input-mini spinner-input", maxlength="30" })
                        @Html.ValidationMessageFor(model => model.MaxQty)
                    </div>            
                </div>
                <div class="control-group">
                    <div class="control-label">
                        @Html.DisplayNameFor(model => model.ImageName)
                    </div>
                    <div class="controls">
                        <table>
                            <tr>
                                <td>
                                    <input type="file" name="fileForImageName" id="fileForImageName" />                                    
                                    @Html.ValidationMessageFor(model => model.ImageName)
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    @if (!string.IsNullOrWhiteSpace(Model.ImageName))
                                    {
                                        string url = Url.Action("image", new {id = Model.ResItemId});
                                        string script = "javascript:popup('" + url + "');";
                                        <label>
                                            <input type="checkbox" value="Yes" id="chbDeleteImage" name="chbDeleteImage" /> 
                                            <span class="lbl"> ลบ</span>
                                        </label>
                                        <br />
                                        <img class="imgBorder" src="@Url.Action("StreamFileFromDisk4", new {Model.ImageName})" 
                                             style="cursor: pointer" onclick='@script' width="300px" />
                                    }
                                    else
                                    {
                                        <img class="imgBorder" src="~/Content/Images/Icon/no_image.png" />
                                    }
                                </td>
                            </tr>
                        </table>
                       
                        
                    </div>            
                </div>
            </div>
        </div>
    </div> 
</div>

<script type="text/javascript"> 
    <!--
    function popup(url) {
        params = 'width=' + screen.width;
        params += ', height=' + screen.height;
        params += ', top=0, left=0';
        params += ', fullscreen=yes';

        newwin = window.open(url, 'windowname4', params);
        if (window.focus) {
            newwin.focus();
        }
        return false;
    }
    
    function genNew() { 
        $('#message_code_generate').html('กำลังประมวลผล');
        var jqxhr = $.getJSON("@Url.Action("GenerateNewCodeJs", "MasResourceItem", new {resSubGrpId})", function() {
            console.log("success");
        })
        .done(function(data) {
            $('#Code').val(data.NewResItemCode);
            $('#message_code_generate').html('ได้รับรหัสพัสดุใหม่เป็น ' + data.NewResItemCode);
        })
        .fail(function() {
            alert("Error");
        });
    }

    function checkCode(){

        $('#message_code_generate').html('กำลังประมวลผล ');
        var url = "@Url.Action("CheckCodeDuplicateJs", "MasResourceItem", new {resItemId = resItemId})&code=" + $('#Code').val();

        var jqxhr = $.getJSON(url, function() {
            console.log("success 2");
        })
        .done(function(data) { 
            if (data.Success == 1) {
                $('#message_code_generate').html("สำเร็จ ไม่พบพัสดุที่ใช้รหัสนี้ สามารถใช้งานได้");
            } else {
                $('#message_code_generate').html(data.ErrorStr);
            }
            
        })
        .fail(function() {
            alert("Error");
        });

    }

// -->
</script> 